package com.yyzw.activiti.modeler.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.yyzw.activiti.modeler.util.ModelNodeJsonData;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Model;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;


@RestController
@RequestMapping(value = "service")
public class ModelerEditController {


    private static final Logger logger = LoggerFactory.getLogger(ModelerEditController.class);

    @Autowired
    RepositoryService repositoryService;

    @Autowired
    ObjectMapper objectMapper;
    /**
     * 返回编辑的文件信息
     * @param modelerId
     * @return
     */
    @RequestMapping(value = "/model/{modelerId}/json",method = RequestMethod.GET,produces = "application/json")
    public ObjectNode getEditorJson(@PathVariable String modelerId){
        ObjectNode modelNode =null;
        Model model = repositoryService.getModel(modelerId);
        if(model!=null){
            try {
                if(StringUtils.isNotEmpty(model.getMetaInfo())){
                    modelNode =(ObjectNode) objectMapper.readTree(model.getMetaInfo());
                }else{
                    modelNode = objectMapper.createObjectNode();
                    modelNode.put(ModelNodeJsonData.MODEL_NAME,model.getName());
                }
                modelNode.put(ModelNodeJsonData.MODEL_ID,model.getId());
                ObjectNode editorJsonNode =(ObjectNode) objectMapper.readTree(new String(repositoryService.getModelEditorSource(model.getId()), "utf-8"));
                modelNode.set("model",editorJsonNode);
            } catch (IOException e) {
                logger.error("Error creating model JSON", e);
                throw new ActivitiException("Error creating model JSON", e);
            }
        }
        return modelNode;
    }


    @RequestMapping(value="/editor/stencilset", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public @ResponseBody
    String getStencilset() {
        InputStream stencilsetStream = this.getClass().getClassLoader().getResourceAsStream("stencilset.json");
        try {
            return IOUtils.toString(stencilsetStream, "utf-8");
        } catch (Exception e) {
            throw new ActivitiException("Error while loading stencil set", e);
        }
    }


    @RequestMapping(value="/model/{modelId}/save",method = RequestMethod.POST)
    @ResponseStatus(value = HttpStatus.OK)
    public void saveModel(@PathVariable String modelId
            , String name, String description
            , String json_xml, String svg_xml, HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, String[]> map= request.getParameterMap();
            JSONObject jsonObject=new JSONObject();
            //全跑到key了，可取方案
            for(Map.Entry<String,String[]> entry:map.entrySet()){
                String data=entry.getKey()+"="+(entry.getValue()[0]);
                jsonObject= JSON.parseObject(data);
            }
            name= (String) jsonObject.get("name");
            description= (String) jsonObject.get("description");
            json_xml= (String) jsonObject.get("json_xml");
            svg_xml= (String) jsonObject.get("svg_xml");

            Model model = repositoryService.getModel(modelId);
            JSONObject object=new JSONObject();
            object.put(ModelNodeJsonData.MODEL_NAME, name);
            object.put(ModelNodeJsonData.MODEL_DESCRIPTION, description);
            model.setMetaInfo(object.toString());
            model.setName(name);
            repositoryService.saveModel(model);

            repositoryService.addModelEditorSource(model.getId(), json_xml.getBytes("utf-8"));

            InputStream svgStream = new ByteArrayInputStream(svg_xml.getBytes("utf-8"));
            TranscoderInput input = new TranscoderInput(svgStream);

            PNGTranscoder transcoder = new PNGTranscoder();
            // Setup output
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            TranscoderOutput output = new TranscoderOutput(outStream);

            // Do the transformation
            transcoder.transcode(input, output);
            final byte[] result = outStream.toByteArray();
            repositoryService.addModelEditorSourceExtra(model.getId(), result);
            outStream.close();

        } catch (Exception e) {
            logger.error("Error saving model", e);
            throw new ActivitiException("Error saving model", e);
        }
    }

}
